# -*- coding: utf-8 -*-
"""
Created on Sun Aug 25 11:52:41 2024

@author: ZHOU
"""
import threading
import time

from pynput import mouse, keyboard  
import pydirectinput 

global m  
m = mouse.Controller()
global move_flag
move_flag=0
global quit_flag
quit_flag = 0
global quit_judg
quit_judg = 0

def callback(k,li):
    global quit_judg
    if "alt" in k:
        quit_judg =1
    
def on_mouse_move(x, y):  
    pass
#    print(f'鼠标移动到位置: ({x}, {y})')  

def on_mouse_click(x, y, button, pressed):  
    global move_flag
    s=str(button)
    if pressed and move_flag == 0:    
        if s=="Button.x1":
            move_flag = 1
        elif s=="Button.x2":
            move_flag = 2

#        print(f'{button} 释放于位置: ({x}, {y})') 
        
def on_mouse_scroll(x, y, dx, dy):
    pass
    # dx 和 dy 表示滚动的水平方向和垂直方向的量  
    # 在大多数鼠标上，只有 dy 会变化，表示垂直滚动  
#    print(f'Scrolled at {x}, {y} ({dx}, {dy})')  
 
 
def on_key_press(key):
    callback(str(key),[]);
  
def release_callback(k):
    global move_flag
    global quit_flag
    global quit_judg
    if k=="'j'" and quit_judg == 1:
       quit_flag=1
       move_flag=0
    elif "alt" in k:
        quit_judg = 0
        
def on_key_release(key):  
    release_callback(str(key))
#    try:  
#        print(f'{key.char} 被释放')  
#    except AttributeError:  
#        print(f'{key} 被释放')  
  
global mouse_listener
global keyboard_listener
# 创建鼠标监听器  
mouse_listener = mouse.Listener(on_move=on_mouse_move, on_click=on_mouse_click,on_scroll=on_mouse_scroll)  

# 创建键盘监听器，同时监听按下和释放事件  
keyboard_listener = keyboard.Listener(on_press=on_key_press, on_release=on_key_release)  

def key_start():
    global mouse_listener
    global keyboard_listener
    mouse_listener.start() 
    keyboard_listener.start() 
    mouse_listener.join()  
    keyboard_listener.join() 
    
global t
t=threading.Thread(target=key_start)
t.setDaemon(True)
t.start()    

def down():
    global move_flag   
    if move_flag == 1:
        m.click(mouse.Button.left)
        time.sleep(0.5)
        m.click(mouse.Button.right)
        time.sleep(0.35)
    elif move_flag ==2 :
        m.click(mouse.Button.left)
        time.sleep(0.03)
        m.click(mouse.Button.right)
        time.sleep(0.35) 
    move_flag=0
    
def move_while():
    global move_flag
    global quit_flag
    while quit_flag==0:
        time.sleep(0.01)
        while move_flag:  
            down()
            
def main():
    global mouse_listener
    global keyboard_listener
    print("按下侧键进行两种识破")
    print("作者: 网易独家音乐人MikeZhou")
    print("按alt+J退出程序")
#    key_start()
    move_while()
    keyboard_listener.stop()
    mouse_listener.stop()
    print("quit")

    
if __name__ == '__main__':
    main()
   